﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using TraductorAVoz.Resources;
using Windows.Phone.Speech.Recognition;
using Windows.Phone.Speech.Synthesis;

namespace TraductorAVoz
{
    public partial class MainPage : PhoneApplicationPage
    {
        // Constructor
        public MainPage()
        {
            InitializeComponent();

            // Sample code to localize the ApplicationBar
            //BuildLocalizedApplicationBar();
        }

        private async void ApplicationBarIconButton_Click(object sender, EventArgs e)
        {

            var recognizer = new SpeechRecognizerUI();
          
            recognizer.Settings.ReadoutEnabled = true;

            var result = await recognizer.RecognizeWithUIAsync();


            if(result.ResultStatus== SpeechRecognitionUIStatus.Succeeded){

                txtOriginal.Text = result.RecognitionResult.Text;

                var traductor =  Resources["ViewModel"] as TraductorAVoz.ViewModel.TraductorAVozViewModel;

                traductor.Disp = Deployment.Current.Dispatcher;

                traductor.TraducirCommand.Execute(new List<string>() { "ES", "EN", txtOriginal.Text });
            }
        
       
         
        }

        private void txtTraduccion_TextChanged(object sender, TextChangedEventArgs e)
        {
            var speaker = new SpeechSynthesizer();
            
            speaker.SetVoice((from v in InstalledVoices.All where v.Language == "en-US" select v).First());

            speaker.SpeakTextAsync(txtTraduccion.Text);
        
        
        }


       

        // Sample code for building a localized ApplicationBar
        //private void BuildLocalizedApplicationBar()
        //{
        //    // Set the page's ApplicationBar to a new instance of ApplicationBar.
        //    ApplicationBar = new ApplicationBar();

        //    // Create a new button and set the text value to the localized string from AppResources.
        //    ApplicationBarIconButton appBarButton = new ApplicationBarIconButton(new Uri("/Assets/AppBar/appbar.add.rest.png", UriKind.Relative));
        //    appBarButton.Text = AppResources.AppBarButtonText;
        //    ApplicationBar.Buttons.Add(appBarButton);

        //    // Create a new menu item with the localized string from AppResources.
        //    ApplicationBarMenuItem appBarMenuItem = new ApplicationBarMenuItem(AppResources.AppBarMenuItemText);
        //    ApplicationBar.MenuItems.Add(appBarMenuItem);
        //}
    }
}